<?php

/**
 *  @author  Lynn Cho <lynncho@live.com>
 *  @createTime  2016-9-1 14:18:20
 *  @copyright  shewen technology
 *  @descrition  
 */
class System extends BaseController {

    public function __construct() {
        parent::__construct();
        $this->load->model('SystemModel');
        $this->load->model('ServiceGroupModel');
    }

    /**
     * manage index page
     */
    public function index() {
//        $func = $this->SystemModel->getFuncIDAndName();//for auto increase menu item
        $permission = $this->SystemModel->getPermission();
        $re['timeHello'] = $this->SystemModel->timeHello();
        $data = array('user' => $this->session->manager, 'data' => $re, 'permission' => $permission);
        $this->load->view('index', $data);
    }

    /**
     * site status
     */
    public function status() {
        echo 'developing';
    }

    /**
     * site setting
     */
    public function setting() {
        echo 'developing';
    }

    /**
     * manager user
     */
    public function manager() {
        $this->load->view('system/manager');
    }

    public function test() {
        $this->load->library('IP');
        var_dump($this->ip->find('31.210.191.255'));
    }

    public function getManagerList() {
        if ($_POST) {
            //权限
            $username = @htmlspecialchars(trim($this->input->post('username')));
            $type = @htmlspecialchars(trim($this->input->post('power')));
            $rows = @htmlspecialchars(trim($this->input->post('rows')));
            $page = @htmlspecialchars(trim($this->input->post('page')));
            $re = $this->SystemModel->getManagerList($username, $type, $rows, $page);
            echo json_encode($re);
        }
    }

    public function addManager() {
        if ($_POST) {
            //权限
            $array['username'] = @htmlspecialchars(trim($this->input->post('username')));
            $array['pwd'] = @htmlspecialchars(trim($this->input->post('pwd')));
            if (!empty($array['username']) && !empty($array['pwd'])) {
                $array['phone'] = @htmlspecialchars(trim($this->input->post('phone')));
                $array['email'] = @htmlspecialchars(trim($this->input->post('email')));
                $array['powerlist'] = $this->input->post('powertree');
                $array['note'] = @htmlspecialchars(trim($this->input->post('note')));
                if ($this->SystemModel->saveManager($array)) {
                    echo true;
                } else {
                    echo json_encode(array('errorMsg' => '插入数据库失败'));
                }
            } else {
                echo json_encode(array('errorMsg' => '用户名或密码不允许为空'));
            }
        }
    }

    public function editManager() {
        if ($_POST) {
            //权限
            $array['id'] = @htmlspecialchars(trim($this->input->get('id')));
            $array['phone'] = @htmlspecialchars(trim($this->input->post('phone')));
            $array['email'] = @htmlspecialchars(trim($this->input->post('email')));
            $array['pwd'] = @htmlspecialchars(trim($this->input->post('pwd')));
            $array['powerlist'] = $this->input->post('powertree');
            $array['grouplist'] = $this->input->post('grouptree');
            $array['note'] = @htmlspecialchars(trim($this->input->post('note')));
            if ($this->SystemModel->editManager($array)) {
                echo true;
            } else {
                echo json_encode(array('errorMsg' => '修改管理员资料失败'));
            }
        }
    }

    public function deleteManager() {
        if ($_POST) {
            //权限
            $id = @htmlspecialchars(trim($this->input->post('id')));
            if ($this->SystemModel->deleteManager($id)) {
                echo true;
            } else {
                echo json_encode(array('errorMsg' => '删除管理员失败'));
            }
        }
    }

    public function getManagerDetailPermissionList() {
        $powerarray = $this->db->select('prvid')->where('mid', @htmlspecialchars(trim($this->input->get('id'))))->get('funcprv')->result_array();
        foreach ($powerarray as $key => $val) {
            $powertree[$key] = (int) $val['prvid'];
        }
        if (empty($powertree)) {
            echo '[]';
        } else
            echo json_encode($powertree);
    }

    public function getManagerGroupPermissionList() {
        $grouparray = $this->db->select('groupid')->where('mid', @htmlspecialchars(trim($this->input->get('id'))))->get('managergroup')->result_array();
        foreach ($grouparray as $key => $val) {
            $grouptree[$key] = (int) $val['groupid'];
        }
        if (empty($grouptree)) {
            echo '[]';
        } else
            echo json_encode($grouptree);
    }

    public function getDetailPermissionTree() {
        $array = $this->SystemModel->getDetailPermissionTree();
        echo json_encode($array);
    }

    public function getGeneralPermissionList() {
        echo json_encode($this->SystemModel->getGeneralPermission());
    }

}
